<?php

/**
 * Description of PanelModel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class PanelModel extends Panel {

    /**
     * The constructor.
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * ADMIN SPECIFIC METHODS.
     */

    /**
     * Getting data for all panels within a given page.
     * Returns paginated results in descending order.
     *
     * @param int $pageId
     * @param int $pageNumber
     * @return array
     */
    public static function getPanelData($pageId, $searchString = NULL, $pageNumber = 1) {

        /**
         * Get paginated results.
         */
        $queryCount = new Query('
            SELECT COUNT(*) AS `count`
            FROM `panel`
            :where
            :and :search;
        ');
        $queryCount = AdminUtils::queryProcessing($queryCount, 'panel', 'page', $pageId, $searchString);
        $query = new Query('
            SELECT :select
            FROM `panel`
            :bridge
            :join
            :where
            :and :search
            ORDER BY `z`.`page` DESC, `panel`.`id`
            LIMIT :offset, :row_count;
        ');
        $query = AdminUtils::queryProcessing($query, 'panel', 'page', $pageId, $searchString);

        $pagination = new Pagination(
            $query,
            $queryCount,
            $pageNumber
        );
        $output = array();
        $output['tabulated'] = $pagination->getRows();
        $output['navigation_array'] = $pagination->getNavigationArray();

        /**
         * Get page, subdomain and domain data.
         */
        if ($pageId != NULL) {
            $page = new Page;
            $page->load($pageId);
            $output['page'] = $page->getViewFields();
            $subdomain = $page->getSubdomain();
            $output['subdomain'] = $subdomain->getViewFields();
            $domain = $subdomain->getDomain();
            $output['domain'] = $domain->getViewFields();
        }

        return $output;
    }

}